package org.pay.manage.merchant.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.pay.core.common.constant.Constant;
import org.pay.core.common.constant.MchConstant;
import org.pay.core.common.constant.RetEnum;
import org.pay.core.common.domain.BizResponse;
import org.pay.core.common.domain.PayPageRes;
import org.pay.core.common.domain.PayResponse;
import org.pay.core.entity.MchPassage;
import org.pay.core.entity.PassageConfig;
import org.pay.manage.common.ctrl.BaseController;
import org.pay.manage.common.service.RpcCommonService;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: yangshancheng
 * @date: 18/02/11
 * @description:
 */
@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/mch_passage")
@PreAuthorize("hasRole('"+ MchConstant.MGR_ROLE_NORMAL+"')")
public class MchPassageController extends BaseController {

    @Autowired
    private RpcCommonService rpcCommonService;

    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchPassage mchPassage = getObject(param, MchPassage.class);
        int count = rpcCommonService.rpcMchPassageService.count(mchPassage);
        if(count == 0) ResponseEntity.ok(PayPageRes.buildSuccess());
        List<MchPassage> mchPassageList = rpcCommonService.rpcMchPassageService.select((getPageIndex(param) -1) * getPageSize(param), getPageSize(param), mchPassage);
        return ResponseEntity.ok(PayPageRes.buildSuccess(mchPassageList, count));
    }

    /**
     * 查询
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Integer id = getIntegerRequired(param, "id");
        MchPassage mchPassage = rpcCommonService.rpcMchPassageService.findById(id);
        return ResponseEntity.ok(PayResponse.buildSuccess(mchPassage));
    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseEntity<?> update(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchPassage mchPassage = getObject(param, MchPassage.class);
        int count = rpcCommonService.rpcMchPassageService.update(mchPassage);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseEntity<?> add(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchPassage mchPassage = getObject(param, MchPassage.class);
        int count = rpcCommonService.rpcMchPassageService.add(mchPassage);
        if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
        return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
    }

    @RequestMapping("/list_passage")
    @ResponseBody
    public ResponseEntity<?> canAddPassge(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long mchId = getLongRequired(param, "mchId");
        MchPassage mchPassage = new MchPassage();
        mchPassage.setMchId(mchId);
        List<Integer> notPassageIds = new LinkedList<>();
        List<MchPassage> mchPassageList = rpcCommonService.rpcMchPassageService.selectAll(mchPassage);
        for(MchPassage mp : mchPassageList) {
            notPassageIds.add(mp.getPassageId());
        }
        List<PassageConfig> passageConfigList = rpcCommonService.rpcPassageConfigService.canAddPassage(notPassageIds);
        return ResponseEntity.ok(PayResponse.buildSuccess(passageConfigList));
    }

}
